# 创建时间:2023/5/2 15:14
# 创建人:天霄
# 计划 :SenjoTwintail
# 文件 :AsyncGenerate.py
# 基于 Python 3.8.2
# ========================================
# 协程同步生成
# ========================================
from vars import *
import asyncio

import pygame

from BasePygame.Gamer import LoadImage, change_image
from BasePygame.Public import fontDefault


class GenerateImageListAsync:
    """仅用于协程同步生成图像"""
    frame_size = (180, 110)
    content_frame = LoadImage(pygame.Surface(frame_size, pygame.SRCALPHA).convert_alpha())
    scenebg_root = CONST.PATH.BG_ROOT / 'scenebg'
    results = dict()

    async def generate_bg(self, index, f):
        """生成1张背景缩略图"""
        path = self.scenebg_root / f
        icon = LoadImage(change_image(path)).resize((140, 80))
        sT = lambda s: fontDefault.render(str(s), 'gray', size=25)[0]
        content = self.content_frame.copy().draw_custom(icon, 'topright').draw_RectCustom(sT(index+1), (0,0,40,80), 'midleft')
        content.objectName = f
        return content

    async def generate_bgs(self):
        """生成背景缩略图对象list"""
        cloth_iconpaths = self.scenebg_root.iterdir()
        coros = [self.generate_bg(i, f.name) for i, f in enumerate(cloth_iconpaths)]
        results = await asyncio.gather(*coros)
        return results

    def generate_bgsGo(self):
        """执行生成背景缩略图"""
        return asyncio.run(self.generate_bgs())


genAsync = GenerateImageListAsync()
